/*
 * FCKeditor - The text editor for internet
 * Copyright (C) 2003-2005 Frederico Caldeira Knabben
 * 
 * Licensed under the terms of the GNU Lesser General Public License:
 * 		http://www.opensource.org/licenses/lgpl-license.php
 * 
 * For further information visit:
 * 		http://www.fckeditor.net/
 * 
 * "Support Open Source software. What about a donation today?"
 * 
 * File Name: Uploader.cs
 * 	This is the code behind of the uploader.aspx page used for Quick Uploads.
 * 
 * File Authors:
 * 		Frederico Caldeira Knabben (fredck@fckeditor.net)
 */

using System ;
using System.Globalization ;
using System.Xml ;
using System.Web ;

using Krystalware.SlickUpload;
using Krystalware.SlickUpload.Status;
using System.IO;
using Adge.Common;

namespace FredCK.FCKeditorV2
{
	public class Uploader : FileWorkerBase
	{
        string hostpath = "http://{0}{1}";

		protected override void OnLoad(EventArgs e)
		{
            UploadStatus status = HttpUploadModule.GetUploadStatus();
            // Get the uploaded file full path.
            string fileFullPath = status.GetUploadedFiles()[0].LocationInfo["fileName"];
            // Get the uploaded file name.
            string sFileName = Path.GetFileName(fileFullPath);            		

			int iErrorNumber = 0 ;
			string sFileUrl = "";
            string fileFullName = fileFullPath.Substring(AdgeConfig.FileSoragePhyPath.Length);

            sFileUrl = string.Format(hostpath, Request.ServerVariables["HTTP_HOST"], ResolveUrl(AdgeConfig.FileSoragePath + fileFullName));

            SendResults(iErrorNumber, ResolveClientUrl(sFileUrl), sFileName);
		}

		#region SendResults Method

		private void SendResults( int errorNumber )
		{
			SendResults( errorNumber, "", "", "" ) ;
		}

		private void SendResults( int errorNumber, string fileUrl, string fileName )
		{
			SendResults( errorNumber, fileUrl, fileName, "" ) ;
		}

		private void SendResults( int errorNumber, string fileUrl, string fileName, string customMsg )
		{
			Response.Clear() ;

			Response.Write( "<script type=\"text/javascript\">" ) ;
			Response.Write( "window.parent.OnUploadCompleted(" + errorNumber + ",'" + fileUrl.Replace( "'", "\\'" ) + "','" + fileName.Replace( "'", "\\'" ) + "','" + customMsg.Replace( "'", "\\'" ) + "') ;" ) ;
			Response.Write( "</script>" ) ;

			Response.End() ;
		}

		#endregion
	}
}
